package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.Context;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入到购物车中的商品是否以及存在了
        //如果已经存在了，只需要将数量加一
        //属性赋值
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        //将userId 通过 线程的方式 获取到
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //判断list  如果list中有值，
        if (list!=null && list.size()>0){
            ShoppingCart cart = list.get(0);
            //进行加1 操作
            cart.setNumber(cart.getNumber()+1);
            //进行更新操作
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //如果不存在，需要插入一条购物车数据
            Long dishId = shoppingCart.getDishId();
            if (dishId!=null){
                //如果是菜品，进行判断
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else {
                Long setmealId = shoppingCart.getSetmealId();
                //否则就是套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void subOneShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前要减去购物车中的商品是否以及存在了
        //如果已经存在了，只需要将数量 减1
        //属性赋值
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        //将userId 通过 线程的方式 获取到
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //判断list  如果list中有值，
        if (list!=null && list.size()>0){
            ShoppingCart cart = list.get(0);
            Long shoppingCartId = cart.getId();
            //如果 cart.getNumber() 购物车中某一订单数量为1 就删除这个订单
            if (cart.getNumber()==1){
                shoppingCartMapper.deleteShoppingCartById(shoppingCartId);
            }else {
                //进行减1 操作
                cart.setNumber(cart.getNumber()-1);
                //进行更新操作
                shoppingCartMapper.updateNumberById(cart);
            }

        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //拿到用户id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        //根据用户id去查找购物车中的数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    @Override
    public void clearShoppingCart() {
        //获取用户的id  userId
        Long userId = BaseContext.getCurrentId();
        //根据用户的删除购物车
        shoppingCartMapper.clearShoppingCart(userId);
    }

}
